package com.bodu.depress.service.impl;

import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.entity.result.Result;
import com.bodu.depress.entity.request.DepressQuestionOptionsSolutionRequest;
import com.bodu.depress.entity.request.DepressQuestionRequest;
import com.bodu.depress.entity.request.DepressSolutionRequest;
import com.bodu.depress.entity.vo.DepressQuestionVo;
import com.bodu.depress.enums.QuestionTypeEnums;
import com.bodu.depress.mapper.DepressSolutionMapper;
import com.bodu.depress.service.DepressQuestionService;
import com.bodu.core.entity.ParamReq;
import com.bodu.depress.mapper.DepressOptionsMapper;
import com.bodu.depress.mapper.DepressQuestionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目 Service impl
 *
 * @author Administrator
 */
@Service
public class DepressQuestionServiceImpl implements DepressQuestionService {

    @Resource
    private DepressQuestionMapper depressQuestionMapper;

    @Resource
    private DepressOptionsMapper depressOptionsMapper;

    @Resource
    private DepressSolutionMapper depressSolutionMapper;

    @Override
    public DepressQuestionVo getModel(Long id) {
        DepressQuestionVo vo = depressQuestionMapper.getModel(id);
        vo.setStatusFlagName(StatusFlagEnum.getStatusValues(vo.getStatusFlag()));
        vo.setQuestionTypeName(QuestionTypeEnums.getStatusValues(vo.getQuestionType()));
        return vo;
    }

    @Transactional
    @Override
    public Long save(DepressQuestionRequest request) {
        depressQuestionMapper.save(request);
        return request.getId();
    }

    @Transactional
    @Override
    public Long update(DepressQuestionRequest request) {
        depressQuestionMapper.update(request);
        return request.getId();
    }

    @Override
    public List<DepressQuestionVo> listPage(ParamReq<DepressQuestionRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<DepressQuestionVo> res = depressQuestionMapper.listPage(params);
        res.forEach(depressQuestionPo -> {
            depressQuestionPo.setStatusFlagName(StatusFlagEnum.getStatusValues(depressQuestionPo.getStatusFlag()));
            depressQuestionPo.setQuestionTypeName(QuestionTypeEnums.getStatusValues(depressQuestionPo.getQuestionType()));
        });
        return res;
    }

    @Override
    public Integer countPage(ParamReq<DepressQuestionRequest> params) {
        return depressQuestionMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return depressQuestionMapper.updateDelFlagById(id, delFlag);
    }

    @Transactional
    @Override
    public Result saveAll(DepressQuestionOptionsSolutionRequest request) {

        DepressQuestionRequest questionRequest = request.getQuestion();
        questionRequest.setCreateUser(request.getCreateUser());
        depressQuestionMapper.save(questionRequest);

        // 难道题目编号
        Long questionId = questionRequest.getId();

        request.getOptions().forEach(option -> {
            option.setQuestionId(questionId);
            option.setCreateUser(request.getCreateUser());
            option.setStatusFlag(StatusFlagEnum.USE.getValue());
            option.setDelFlag(DelFlagEnum.NO.getValue());
            depressOptionsMapper.save(option);
        });

        DepressSolutionRequest solution = request.getSolution();
        solution.setQuestionId(questionId);
        solution.setCreateUser(request.getCreateUser());
        solution.setStatusFlag(StatusFlagEnum.USE.getValue());
        solution.setDelFlag(DelFlagEnum.NO.getValue());
        depressSolutionMapper.save(solution);
        return Result.success("保存成功！");
    }

    @Transactional
    @Override
    public Result<Integer> updateAll(DepressQuestionOptionsSolutionRequest request) {
        DepressQuestionRequest questionRequest = request.getQuestion();
        depressQuestionMapper.update(questionRequest);

        request.getOptions().forEach(option -> depressOptionsMapper.update(option));

        DepressSolutionRequest solution = request.getSolution();
        depressSolutionMapper.update(solution);
        return Result.success("修改成功！");
    }
}
